<template>
  <el-dialog :visible.sync="visible" title="需求申请" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item prop="pcode" label="目录级别" class="menu-list">
        <el-popover v-model="menuListVisible" ref="menuListPopover" placement="bottom-start" trigger="click">
          <el-tree
            :data="catalogueTree"
            :props="{ label: 'name', children: 'children' }"
            node-key="id"
            ref="menuListTree"
            :highlight-current="true"
            :expand-on-click-node="false"
            accordion
            @current-change="menuListTreeCurrentChangeHandle">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.pcode"  class="icon-input" v-popover:menuListPopover :readonly="true" placeholder="目录级别">
          <i v-if="dataForm.pid !== '0'" slot="suffix" @click.stop="deptListTreeSetDefaultHandle()" class="el-icon-circle-close el-input__icon"></i>
        </el-input>
      </el-form-item>
      <!-- <el-form-item label="目录级别" prop="pcode">
        <el-select v-model="dataForm.pcode" placeholder="请选择">
          <el-option label="人人" value="0"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="需求名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="需求内容" prop="context">
        <el-input type="textarea" v-model="dataForm.context"></el-input>
      </el-form-item>
      <el-form-item label="需求建议
" prop="suggest">
        <el-input type="textarea" v-model="dataForm.suggest"></el-input>
      </el-form-item>
      <el-form-item label="能力描述" prop="descripition">
        <el-input type="textarea" v-model="dataForm.descripition" placeholder="描述"></el-input>
      </el-form-item>
      <!-- <el-form-item label="图片" prop="images">
        <el-input v-model="dataForm.images" placeholder="图片"></el-input>
      </el-form-item> -->
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { mapState } from 'vuex'
export default {
  data () {
    return {
      visible: false,
      menuListVisible: false,
      dataForm: {
        id: '',
        pid: '',
        pcode: '',
        name: '',
        context: '',
        suggest: '',
        descripition: '',
        images: '',
        isDelete: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        files: ''
      }
    }
  },
  computed: {
    ...mapState(['catalogueTree']),
    dataRule () {
      return {
        pcode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        context: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        suggest: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        descripition: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        images: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 上级菜单树, 选中
    menuListTreeCurrentChangeHandle (data) {
      this.dataForm.pid = data.id
      this.dataForm.pcode = data.name
      this.menuListVisible = false
    },
    // 上级菜单树, 设置默认值
    deptListTreeSetDefaultHandle () {
      this.dataForm.pid = '0'
      this.dataForm.pcode = '目录级别'
    },
    // 获取信息
    getInfo () {
      this.$http.get('/demand/bsdemand/' + this.dataForm.id).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs.dataForm.validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/demand/bsdemand/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { leading: true, trailing: false })
  }
}
</script>
